/*
 * Copyright (c) 2016. 恒昌互联网运营中心.
 */

package com.credithc.eagleeye.monitor;
import com.credithc.eagleeye.config.EagleESMonitorConfig;
import com.credithc.framework.es.util.ElasticSearchUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
/**
 *
 * <br>类 名: IceServerMonitorQueueTask
 * <br>描 述: ice Server monitor 任务 定时抽取数据 推送到监控平台
 * <br>作 者: hy
 * <br>创 建： 2015-8-5
 * <br>版 本：v1.0.0
 * <br>
 * <br>历 史: (版本) 作者 时间 注释
 */
public class EagleESMonitorQueueTask implements Runnable {

	private final Logger logger = LoggerFactory.getLogger(EagleESMonitorQueueTask.class);
	private ElasticSearchUtil esUtil;


	/**
	 * @param esUtil
	 */
    public EagleESMonitorQueueTask(ElasticSearchUtil esUtil) {
	    super();
	    this.esUtil = esUtil;
    }

	@Override
	public void run() {
		while (true) {
			try {
				//System.out.println("开始获取队列中数据.....");
				logger.debug("EagleEye开始获取日志队列中数据.....");
				int count=0;
				List<String> jsonStringList = new ArrayList<String>();
				String jsonString=EagleMonitorMgr.getInstance().poll();
				while(jsonString!=null){
					jsonStringList.add(jsonString);
					jsonString=EagleMonitorMgr.getInstance().poll();
					//达到推送阈值大小,调用推送
					if (jsonStringList.size() == EagleESMonitorConfig.EAGLE_MONITOR_ES_BATCH_SIZE) {
						this.pushElasticSearch(jsonStringList);
						count+=jsonStringList.size();
						jsonStringList.clear();
					}
				}
				//把最后不满阈值大小的再次发送
				if(jsonStringList.size()>0){
					this.pushElasticSearch(jsonStringList);
					count+=jsonStringList.size();
				}
				logger.debug(String.format("EagleEye结束获取日志队列中数据[%s条],睡眠%s秒钟",count,EagleESMonitorConfig.EAGLE_MONITOR_SLEEP_MILLIS/1000));

				Thread.sleep(EagleESMonitorConfig.EAGLE_MONITOR_SLEEP_MILLIS);

			} catch (Exception e) {
				logger.error(String.format("定时推送EagleEye monitor error : %s", e.getMessage()));
				e.printStackTrace();
			}
		}
	}

	/**
	 * <br>
	 * 描 述：调用elasticSearch推送EagleEye monitor <br>
	 * 作 者：hy <br>
	 * 历 史: (版本) 作者 时间 注释
	 *
	 * @param jsonStringList
	 */
	private void pushElasticSearch(List<String> jsonStringList) {
		if(jsonStringList!=null&&jsonStringList.size()>0){
			SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy.MM.dd");
			String es_index_name=EagleESMonitorConfig.EAGLE_MONITOR_ES_INDEX+"-"+dateFormat.format(new Date());
			// 失败最多重试3次
			for (int i = 0; i < EagleESMonitorConfig.EAGLE_MONITOR_TRY_TIMES; i++) {
				try {
					esUtil.insertBatchDocument(es_index_name, EagleESMonitorConfig.EAGLE_MONITOR_ES_TYPE, jsonStringList);

				} catch (Exception ex) {
					ex.printStackTrace();
					logger.error(String.format("EagleEye Monitor Error：%s", ex.getMessage()));
					continue;
				}

				logger.debug(String.format("EagleEye Monitor elastic index:%s ,list size :%s", EagleESMonitorConfig.EAGLE_MONITOR_ES_INDEX,
				        jsonStringList.size()));
				break;
			}
		}
	}
}
